package com.parking.platform.controller;


import cn.hutool.core.bean.BeanUtil;
import com.parking.common.core.model.PageInfo;
import com.parking.common.core.model.Result;
import com.parking.common.core.util.JwtUtil;
import com.parking.common.web.aop.Permission;
import com.parking.common.web.util.RedisUtil;
import com.parking.platform.dto.PlatformRoleDto;
import com.parking.platform.dto.RoleNameDto;
import com.parking.platform.entity.PlatformRole;
import com.parking.platform.form.SelRoleForm;
import com.parking.platform.form.UpPlatformStateForm;
import com.parking.platform.service.PlatformRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 香菜狗带
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/platformRole")
@Api(tags = "平台角色接口")
public class PlatformRoleController {
    @Autowired
    private PlatformRoleService platformRoleService;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 最高管理员
     * 新增角色接口
     * 参数：
     * 角色名称：roleName
     *
     * */
    @ApiOperation("新增角色接口")
    @Permission(resources = {"/addPlatformRole.platform"})
    @GetMapping("/addPlatformRole.platform")
    public Result addPlatformRole(String roleName,@RequestHeader("authorization")String authorization) throws Exception {
        //操作人
        String refreshToken = redisUtil.get(authorization);
        Integer adminId = JwtUtil.getPlatformId(refreshToken);
        platformRoleService.addPlatformRole(roleName,adminId);
        return Result.getResult().setCode(200).setMessage("新增角色成功");
    }
    /**
     * 最高管理员
     * 修改角色接口
     * 参数：
     * 角色名称：roleName
     *
     * */
    @ApiOperation("修改角色接口")
    @Permission(resources = {"/upPlatformRole.platform"})
    @GetMapping("/upPlatformRole.platform")
    public Result upPlatformRole(String roleName,Integer platformRoleId,@RequestHeader("authorization")String authorization) throws Exception {
        //操作人
        String refreshToken = redisUtil.get(authorization);
        Integer adminId = JwtUtil.getPlatformId(refreshToken);
        platformRoleService.upPlatformRole(roleName,platformRoleId,adminId);
        return Result.getResult().setCode(200).setMessage("修改角色成功");
    }
    /**
     * 最高管理员
     * 启用角色状态
     * 参数
     * 令牌：authorization
     * 账号状态：state
     * */
    @ApiOperation("启用角色状态接口")
    @Permission(resources = {"/enableRoleState.platform"})
    @GetMapping("/enableRoleState.platform")
    public Result enableRoleState(Integer platformRoleId,@RequestHeader("authorization")String authorization) throws Exception {
        //操作人
        String refreshToken = redisUtil.get(authorization);
        Integer adminId = JwtUtil.getPlatformId(refreshToken);
        platformRoleService.enableRoleState(platformRoleId,adminId);
        return Result.getResult().setCode(200).setMessage("启用角色成功");
    }
    /**
     * 禁用角色状态
     * 参数
     * 令牌：authorization
     * 账号状态：state
     * */
    @GetMapping("/downRoleStateById.platform")
    @ApiOperation("/禁用角色状态接口")
    @Permission(resources = {"/downRoleStateById.platform"})
    public Result  downRoleState(Integer platformRoleId,@RequestHeader("authorization")String authorization) throws Exception {
        //操作人
        String refreshToken = redisUtil.get(authorization);
        Integer adminId = JwtUtil.getPlatformId(refreshToken);
        platformRoleService.downRoleState(platformRoleId,adminId);
        return Result.getResult().setCode(200).setMessage("禁用角色状态成功");
    }
    /**
     * 删除用户
     * 参数
     * 令牌：authorization
     * 账号状态：state
     * */
    @GetMapping("/deleteRoleStateById.platform")
    @Permission(resources = {"/deleteRoleStateById.platform"})
    @ApiOperation("/删除角色接口")
    public Result  deleteRoleState(Integer platformRoleId,@RequestHeader("authorization")String authorization) throws Exception {
        //操作人
        String refreshToken = redisUtil.get(authorization);
        Integer adminId = JwtUtil.getPlatformId(refreshToken);
        platformRoleService.deleteRoleState(platformRoleId,adminId);
        return Result.getResult().setCode(200).setMessage("删除角色成功");
    }
    /**
     * 查询全部角色
     *  参数
     *   角色名：platformRoleName
     *  pageNum:当前页数
     *  pageSize:当前条数
     *  state:状态
     * 返回 PlatformRoleDto
     * */
    @PostMapping("/findRoleAll.platform")
    @ApiOperation("/查询全部角色成功")
    @Permission(resources = {"/findRoleAll.platform"})
    public Result findRoleAll(@RequestBody SelRoleForm form){
        PageInfo pageInfo = platformRoleService.findRoleAll(form);
        return Result.getResult(PageInfo.class).setCode(200).setMessage("查询全部角色成功").setData(pageInfo);
    }
    /**
     * 查询全部角色
     *  无参数
     * 返回 RoleNameDto
     * */
    @GetMapping("/findRoleName.platform")
    @ApiOperation("/查询全部角色成功")
    @Permission(resources = {"/findRoleName.platform"})
    public Result findRoleName(){
        List<RoleNameDto> dto = platformRoleService.findRoleName();
        return Result.getListResult(RoleNameDto.class).setCode(200).setMessage("查询全部角色成功").setData(dto);
    }
    /**
     * 最高管理员
     * 修改角色接口
     * 参数：
     * 角色名称：roleName
     *
     * */
    @ApiOperation("修改角色查看权限接口")
    @Permission(resources = {"/upPlatformPower.platform"})
    @GetMapping("/upPlatformPower.platform")
    public Result upPlatformPower(Integer power,Integer platformRoleId,@RequestHeader("authorization")String authorization) throws Exception {
        System.out.println(power+"参数"+platformRoleId);
        String refreshToken = redisUtil.get(authorization);
        Integer adminId = JwtUtil.getPlatformId(refreshToken);
        platformRoleService.upPlatformPower(power,platformRoleId,adminId);
        return Result.getResult().setCode(200).setMessage("修改角色查看权限成功");
    }

}

